package com.xywl.escort.adapter;

import android.support.annotation.Nullable;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.xywl.escort.R;
import com.xywl.escort.bean.ProjectSpecResp;

import java.util.HashMap;
import java.util.List;

import timber.log.Timber;

public class DeliverySpecAdapter extends BaseQuickAdapter<ProjectSpecResp.DataBean, BaseViewHolder> {
    public DeliverySpecAdapter(int layoutResId, @Nullable List<ProjectSpecResp.DataBean> data) {
        super(layoutResId, data);
    }

    @Override
    protected void convert(BaseViewHolder helper, final ProjectSpecResp.DataBean item) {
        helper.setText(R.id.text_spec_title, item.name);

        RecyclerView recyclerView = helper.getView(R.id.recycler_spec_detail);
        GridLayoutManager manager = new GridLayoutManager(mContext, 4);
        recyclerView.setLayoutManager(manager);

        final List<ProjectSpecResp.DataBean.ProjectStandSmallListBean> list = item.projectStandSmallList;
        DeliverySpecDetailAdapter adapter = new DeliverySpecDetailAdapter(R.layout.item_specific_view, list);
        recyclerView.setAdapter(adapter);

        final HashMap<String, ProjectSpecResp.DataBean.ProjectStandSmallListBean> map = item.map;
        final String key = item.id;

        adapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                ProjectSpecResp.DataBean.ProjectStandSmallListBean specific = (ProjectSpecResp.DataBean.ProjectStandSmallListBean) adapter.getData().get(position);
                Timber.tag("SpecificAdapter").e("specific=" + specific);
                map.put(key, specific);

                String name = specific.name;
                for (int i = 0; i < list.size(); i++) {
                    list.get(i).isClick = list.get(i).name.equals(name);
                }
                adapter.notifyDataSetChanged();
            }
        });
    }
}
